// pagesGoods/detail/index.js
const app = getApp()
Page({
  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    goodsId:'',
    goodsInfo: {},
    car_total:0,
    userInfo:"",
    indicatorDots: true, // 是否显示面板指示点
    autoplay: true, // 自动播放
    interval: 3000, // 自动切换时间间隔
    duration: 400, // 滑动动画时长
    circular: true, //是否循环 是否采用衔接滑动
    indicatorColor: "lightgray", //指示点颜色
    indicatorActiveColor: "red", //当前选中的指示点颜色
    controls: false, // 是否显示播放icon
    share_money:0, //加价金额
    recommendId:0,
  },
  onShow() {
    this.goodsInfo()

    if(this.getStorage('user_info')){      
      this.setData({
        userInfo: this.getStorage('user_info')
      })
      this.goodsCarNum()
    }
  },
  
  shop_index(){
    getApp().storageId=this.data.goodsInfo.storageId
      wx.navigateTo({
        url: '/pagesShop/shop/index',
          success: function (e) {
          let page = getCurrentPages().pop();
          page.onShow();
        }  
      })
  },

  shop_car(){
    this.router('/pages/shopping-cart/index','switchTab')
  },
  

  caredit(e){
    ////问题
    this.goodsCarNum()

  },

  delcar(e){
    this.setData({
      car_total:0     
    })
  },

  //获取手机号 方法
  getPhoneNumber (e) {
    console.log(e)
    var ivObj = e.detail.iv
    var telObj = e.detail.encryptedData
    var that = this;
    wx.login({
      success: res => {        
        var data = {
          token:that.data.userInfo.wx_openid,
          code: res.code,
          encryptedData: telObj,
          iv: ivObj
        }
        that.getPhone(data).then(res =>{
          if(res.resCode=='9999'){
            that.setStorage('user_info', res.data);
            that.setData({
              userInfo:res.data
            })
          }else{
            wx.showToast({
              title: '请重新试一下！',
              icon:'none'
            })
          }
        })
      }
    }) 
  },
  //获取购物车数量
  goodsCarNum(){
      let that=this
      let obj_data={
        token:this.getStorage('user_info').wx_openid,
        storageId:this.getStorage("shopInfo").storageId
      }
      this.goodsCar_num(obj_data).then(res=>{
         if(res.resCode=="9999"){
           that.setData({
             car_total:res.data.car_num
           })
         }
      })
  },
  //获取商品信息
  goodsInfo(){
    let that=this 
    let user_info=this.getStorage('user_info')
    let obj_data={
      goodsId:this.data.goodsId,
      share_money:this.data.share_money,
      is_share:1,
    }
    if(user_info){
      obj_data.token=user_info.wx_openid
    }
    this.goods_info(obj_data).then(res =>{
        if(res.resCode=="9999"){
          that.setData({
            goodsInfo:res.data
          })
        }else{
          wx.showToast({title:res.resMessage,icon:"none"})
        }
    })
  },
  //获取属性信息
  showSpecs(e){
    let that=this
    let user_info=this.getStorage('user_info')
    let type=e.currentTarget.dataset.stype
    let recommendId=this.data.recommendId
    let data={
      goodsId:this.data.goodsInfo.id,
      share_money:this.data.share_money,
      is_share:1,      
    }  
    if(user_info){
      data.token=user_info.wx_openid
    }   
    this.goodsAttr(data).then(res =>{
      if(res.resCode=='9999'){
        this.selectComponent("#bizSpecs").showSpecs(res.data,type,1,recommendId)
      }else{
        wx.showToast({
          title: '系统繁忙，请稍后重试！',
          icon:"none",
        })
      }
    })   
  },

   //获取优惠券
   showCoupon(){     
    let data={
      token:this.getStorage('user_info').wx_openid,
      goodsId:this.data.goodsId
    }  
    let that=this
    this.couponList(data).then(res =>{
    
      if(res.resCode=='9999'){
        this.selectComponent("#coupon").showSpecs(res.data.list)
      }else{
        wx.showToast({
          title: '系统繁忙，请稍后重试！',
          icon:"none",
        })
      }
    })   
  },

  specs_pay(event) {  
    console.log(event)
    this.setData({
      car_total:event.detail.car_total      
    })
  },
  onLoad(event){
    this.setData({
      goodsId:event.id,
      share_money:event.share_money,
      recommendId:event.recommendId
    })   
  },

  //商品评价
  showEvaluate(){
    this.router('/pagesShop/evaluate/index?goodsId='+this.data.goodsId)
  },


  // 播放
  videoPlay: function() {
    console.log("开始播放")
    var videoplay = wx.createVideoContext("video");
    videoplay.play()
    this.setData({
      controls: true,
      autoplay:false
    })
  },

   // 结束播放
   endPlay() {
    console.log("结束播放")
    var videoplay = wx.createVideoContext("video");
    videoplay.pause()
    this.setData({
      controls: false,
      autoplay: true
    })
  },

  

 
})